package com.wanxi.controller;

import com.wanxi.entity.MessageDetailsEntity;
import com.wanxi.entity.PageClassifyEntity;
import com.wanxi.service.MessageDetailsService;
import com.wanxi.service.PageClassifyService;
import com.wanxi.service.impl.MessageDetailsServiceImpl;
import com.wanxi.service.impl.PageClassifyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * message:在线留言
 * @Author : 林子淳
 * @create 2023/2/21 10:20
 */
@WebServlet("/message")
public class MessageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1）处理乱码
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        //2）得到前端数据
        //3）封装到model（实体类JavaBean）
        //4）调用逻辑层处理逻辑关系,将获得的数据通过JSON以对象的形式传到前端页面;
        //导航栏
        PageClassifyService pageClassifyService = new PageClassifyServiceImpl();
        //5）处理完后，得到结果
        //导航栏
        List<PageClassifyEntity> pageList = pageClassifyService.selectServic();
        req.setAttribute("pageList",pageList);
        req.getRequestDispatcher("/jsp/message.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1）处理乱码
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        //2）得到前端数据
        //3）封装到model（实体类JavaBean）
        //4）调用逻辑层处理逻辑关系,将获得的数据通过JSON以对象的形式传到前端页面;
        //导航栏
        PageClassifyService pageClassifyService = new PageClassifyServiceImpl();
        //5）处理完后，得到结果
        //导航栏
        List<PageClassifyEntity> pageList = pageClassifyService.selectServic();
        req.setAttribute("pageList",pageList);
        req.getRequestDispatcher("/jsp/message.jsp").forward(req,resp);



        //获取字符输入流
//        BufferedReader br = req.getReader();
        //读取数据
//        String s = br.readLine();
//        System.out.println("我是在线留言接收到了信息"+s);

//        Map<String, String[]> parameterMap = req.getParameterMap();
//        String[] names = parameterMap.get("name");
//        String[] emails = parameterMap.get("email");
//        String[] contents = parameterMap.get("content");
//        System.out.println(names[0]);
//        System.out.println(emails[0]);
//        System.out.println(contents[0]);
        String name = req.getParameter("name");
        String email = req.getParameter("email");
        String content = req.getParameter("content");
        List<MessageDetailsEntity> messageDetailsEntities = new ArrayList<>();
        messageDetailsEntities.add(MessageDetailsEntity.builder().name(name).email(email).content(content).build());
        MessageDetailsService messageService = new MessageDetailsServiceImpl();
        messageService.add(messageDetailsEntities);
//        System.out.println(name);
//        System.out.println(email);
//        System.out.println(content);
        req.getRequestDispatcher("/jsp/message.jsp").forward(req,resp);
    }
}
